package org.jeecg.modules.maintain.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.common.IDeviceRunningStatusChange;
import org.jeecg.modules.demo.mes.entity.ManageDevice;
import org.jeecg.modules.demo.mes.service.IManageDeviceService;
import org.jeecg.modules.maintain.entity.CamelliaDeviceReport;
import org.jeecg.modules.maintain.entity.CamelliaDeviceRunHistory;
import org.jeecg.modules.maintain.entity.CamelliaDeviceRunTime;
import org.jeecg.modules.maintain.service.ICamelliaDeviceReportService;
import org.jeecg.modules.maintain.service.ICamelliaDeviceRunHistoryService;
import org.jeecg.modules.maintain.service.ICamelliaDeviceRunTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;

@Api(tags="设备运行时间表")
@RestController
@RequestMapping("/maintain/device")
@Slf4j
public class DeviceController implements IDeviceRunningStatusChange {
    @Autowired
    private ICamelliaDeviceRunTimeService camelliaDeviceRunTimeService;  //当前

    @Autowired
    private ICamelliaDeviceRunHistoryService camelliaDeviceRunHistoryService;//历史

    @Autowired
    private IManageDeviceService manageDeviceService;

    @Autowired
    private ICamelliaDeviceReportService camelliaDeviceReportService;

    public Object add(String deviceName){
        ManageDevice device= camelliaDeviceRunTimeService.selDevice(deviceName);
        CamelliaDeviceRunTime cc = camelliaDeviceRunTimeService.sel(device.getDeviceCode());
        //添加到设备运行时长表
        CamelliaDeviceRunTime camelliaDeviceRunTime= new CamelliaDeviceRunTime();
        camelliaDeviceRunTime.setDeviceCode(device.getDeviceCode());
        camelliaDeviceRunTime.setStartTime(new Date());
        camelliaDeviceRunTime.setStates("开机");

//        long times= getTime(cc.getStartTime(),new Date());
//       camelliaDeviceRunTime.setDaytime( String.valueOf(times));
        Boolean Flag=false;
        if(cc==null){
            camelliaDeviceRunTime.setDescibles(device.getDaviceDay().toString());
            camelliaDeviceRunTime.setWeibao(deviceName);
            Flag=  camelliaDeviceRunTimeService.save(camelliaDeviceRunTime);

        }else{
            camelliaDeviceRunTime.setId(cc.getId());
            camelliaDeviceRunTime.setDescibles(device.getDaviceDay().toString());
            Flag=camelliaDeviceRunTimeService.updateById(camelliaDeviceRunTime);
        }
        return  Flag;
    }

    public Object update(String deviceName){
        ManageDevice device= camelliaDeviceRunTimeService.selDevice(deviceName);
        //修改设备运行时长的结束时间
        CamelliaDeviceRunTime cc = camelliaDeviceRunTimeService.sel(device.getDeviceCode());
        cc.setStates("关机");
        cc.setEndTime(new Date());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime start = LocalDateTime.parse(  sdf1.format(cc.getStartTime() ));
//        LocalDateTime end = LocalDateTime.parse( sdf1.format(new Date()  ));
//        int times = getDifferHour(cc.getStartTime(),new Date() );

        long times= getTime(cc.getStartTime(),new Date());
        //between的用法是end-start的时间，若start的时间大于end的时间，则所有的值是负的
//        Duration duration = Duration.between(start, end);
        cc.setDuration(times+cc.getDuration());

        //维保时间
        cc.setDaytime( Long.toString(times+ Integer.parseInt(cc.getDaytime()))  );


        //添加到设备运行时长历史表
        CamelliaDeviceRunHistory camelliaDeviceRunHistory =new CamelliaDeviceRunHistory();
        camelliaDeviceRunHistory.setStartTime(cc.getStartTime());
        camelliaDeviceRunHistory.setEndTime(cc.getEndTime());
        camelliaDeviceRunHistory.setRunTime(Long.toString(times));
        camelliaDeviceRunHistory.setDesca( device.getDeviceCode());
        camelliaDeviceRunHistory.setNames(deviceName);

        camelliaDeviceRunHistoryService.save(camelliaDeviceRunHistory);
        if(device.getDaviceDay()-Integer.parseInt(cc.getDaytime()) <=0){
            cc.setDaytime("0");
            device.setState(2);
//            cc.setStates("维保中");
            //设备维保时长到期 修改设备状态为在维保
            manageDeviceService.updateById(device);
            //设备维保时长到期 添加到设备维保表
            CamelliaDeviceReport camelliaDeviceReport =new CamelliaDeviceReport();
            camelliaDeviceReport.setUserName("设备上报");//设备编号
            camelliaDeviceReport.setDeviceCode(device.getDeviceCode());
            camelliaDeviceReport.setProblemDesc("维保时长到期!");
            camelliaDeviceReport.setDeviceAddress(0);
            camelliaDeviceReport.setDegree(0);
            camelliaDeviceReport.setReportTime(new Date());
            String number="BX"+sendReport();
            camelliaDeviceReport.setReportCode(number);
            camelliaDeviceReportService.save(camelliaDeviceReport);
        }

        boolean falg=    camelliaDeviceRunTimeService.updateById(cc);
        return  falg;
    }

    //生成报修号
     private String sendReport(){
         SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
         String number = sdf.format(System.currentTimeMillis());//202211041552160219
//         System.out.println("根据当前时间戳生成唯一："+number);
////         //补充一个三位随机数
////         int x=(int)(Math.random()*900)+100;
//         String serialNumber = number + x;
        return number;
     }

    // 24 * (differ / dayM) 这里拿到被舍弃的整数，整数是几，就代表相隔几天，一天24小时，那就整数乘以24即可。
    private static int getDifferHour(Date startDate, Date endDate) {
        long dayM = 1000 * 24 * 60 * 60;
        long hourM = 1000 * 60 * 60;
        long differ = endDate.getTime() - startDate.getTime();
        long hour = differ % dayM / hourM + 24 * (differ / dayM);
        return Integer.parseInt(String.valueOf(hour));
    }
    public static long getTime(Date oldTime,Date newTime)  {
        long NTime =newTime.getTime();
        //从对象中拿到时间
        long OTime = oldTime.getTime();
        long diff=(NTime-OTime)/1000/60;
        return diff;
    }

    @Override
    public void open(String deviceName) {
        log.info("设备运行,{}",deviceName);
        add(deviceName);
    }

    @Override
    public void close(String deviceName) {
        log.info("设备停止,{}",deviceName);
        update(deviceName);
    }
}
